package com.huibei.wateradmin.authority.controller;

import com.huibei.wateradmin.authority.message.ClientMenuTreeInfo;
import com.huibei.wateradmin.authority.message.ClientRoleInfo;
import com.huibei.wateradmin.authority.message.ReqDateTablesMsg;
import com.huibei.wateradmin.authority.message.ReqUpdateRoleMsg;
import com.huibei.wateradmin.authority.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by wanglinlin on 2016/7/16.
 */
@RestController
@RequestMapping(value = "/admin/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/listPage",method = RequestMethod.GET)
    public ModelAndView listPage() {
        ModelAndView mv = new ModelAndView("role/list");
        return mv;
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Page<ClientRoleInfo> list(ReqDateTablesMsg msg) {
        return roleService.findByPage(new PageRequest(msg.getPageNum(),msg.getLength()));
    }

    @RequestMapping(value = "/roleMenus", method = RequestMethod.GET)
    public List<ClientMenuTreeInfo> roleMenus(Long roleId) {
        return roleService.findMenusForTreeByRoleId(roleId);
    }

    @RequestMapping(value = "/updateRole",method = RequestMethod.POST)
    public void updateRole(ReqUpdateRoleMsg msg) {
        System.out.println(msg.toString());
        roleService.updateMenus(msg.getRoleId(),msg.getDescription(),msg.getName(),msg.getMenuIds());
    }

    @RequestMapping(value = "/roleInfo/{id}")
    public ClientRoleInfo getRoleInfo(@PathVariable Long id) {
        return roleService.findById(id);
    }

}
